package com.itcanteen.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcanteen.reggie.common.R;
import com.itcanteen.reggie.dto.DishDto;
import com.itcanteen.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2022/10/26 16:05
 */

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 作用:保存菜品数据
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto, HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("user");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        //2.把数据交给service
        dishService.save(dishDto);
        return R.success("添加成功");
    }

    /**
     * 请求路径 http://localhost:8080/dish/page?page=1&pageSize=10&name=a
     请求方式: GET
     *
     *
     */
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer pageSize,String name){
        Page<DishDto> pageResult = dishService.findByPage(page,pageSize,name);
        return R.success(pageResult);
    }


    /**
     * 请求路径：  http://localhost:8080/dish/1397850140982161409
     * 请求方式:  GET
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }


    /**
     * 请求路径：  http://localhost:8080/dish/
     * 请求方式:  PUT
     * 作用：修改菜品
     */
    @PutMapping
    public R updateDish(@RequestBody  DishDto dishDto,HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("user");
        dishDto.setUpdateUser(empId);
        //2. 交给service
        dishService.updateDish(dishDto);
        return R.success("修改菜品成功");
    }
}
